package com.azusa.ktc_spit.service;

import com.azusa.ktc_spit.dao.SpitDao;
import com.azusa.ktc_spit.dao.ThumbupDao;
import com.azusa.ktc_spit.entity.Spit;
import com.azusa.ktc_spit.entity.Thumbup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.IdWorker;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Description 职位 服务层
 * @author Azusa
 * @date 2020-10-10 18:33:43
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class SpitServiceImpl implements SpitService {

	@Autowired
	private SpitDao spitDao;

	@Autowired
	private IdWorker idWorker;

	@Autowired
	private ThumbupDao thumbupDao;

	//注入mongoTemplate操作对象
	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	* 查询全部列表
	* @return
	*/
	@Override
	public List<Spit> findAll() {
		return spitDao.findAll();
	}


	/**
	* 根据ID查询实体
	* @param id
	* @return
	*/
	@Override
	public Spit findById(String id) {
		return spitDao.findById(id).get();
	}

	/**
	* 增加
	* @param spit
	*/
	@Override
	public void add(Spit spit) {
		spit.setId( idWorker.nextId()+"" );
		spitDao.save(spit);
	}

	/**
	* 修改
	* @param spit
	*/
	@Override
	public void update(Spit spit) {
		spitDao.save(spit);
	}

	/**
	 *	根据条件查询Spit列表
	 * @param spit
	 * @return
	 */
	@Override
	public List<Spit> findSearch(Spit spit) {
		return spitDao.findAll(Example.of(spit));
	}

	/**
	 * 根据条件查询Spit分页列表
	 * @param pageNum
	 * @param pageSize
	 * @param spit
	 * @return
	 */
	@Override
	public Page<Spit> findSearchPage(int pageNum, int pageSize, Spit spit) {
		return spitDao.findAll(Example.of(spit),PageRequest.of(pageNum - 1,pageSize));
	}

	/**
	* 删除
	* @param id
	*/
	@Override
	public void deleteById(String id) {
		spitDao.deleteById(id);
	}


	/**
	 * 吐槽点赞
	 * @param userId
	 * @param spitId
	 */
	@Override
	public void thumbUpSpit(String userId,String spitId){
		Thumbup thumbup = new Thumbup(null, userId, spitId);
		// 1.查看用户是否已经点赞过了
		Optional<Thumbup> isThumbUp = thumbupDao.findOne(Example.of(thumbup));
		int i = 1;
		// 2.如果点赞过则spit的thumbup-1，同时删除thumbUp中的相应数据
		if (isThumbUp.isPresent()){
			i = -1;
			thumbupDao.deleteById(isThumbUp.get().getId());
		}else {
			// 3.如果没有点赞则spit的thumbup+1，同时在thumbUp增加一条记录
			thumbup.setId(idWorker.nextId() + "");
			thumbupDao.save(thumbup);
		}
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(spitId));
		Update update = new Update();
		update.inc("thumbup",i);
		mongoTemplate.updateFirst(query,update,Spit.class);
	}

	@Override
	public Page<Spit> findComment(int pageNum,int pageSize,String parentId){
		return spitDao.findByParentid(parentId,PageRequest.of(pageNum - 1,pageSize));
	}
}
